<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Tree;

class CategoriesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('图书分类列表');

            $content->body(Category::tree(function ($tree) {
                $tree->query(function ($model) {
                    return $model->where('deleted', 0);
                });

                $tree->branch(function ($branch) {
                    $logo = '';
                    if(!empty($branch['logo']))
                    {
                        $src = config('admin.upload.host').'/'.$branch['logo'];
                        $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";
                    }

                    return "category_id:{$branch['id']} - {$branch['title']} $logo category_parent_id:{$branch['parent_id']}";
                });
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('修改分类');

            $content->body($this->form()->edit($id));
        });
    }

    public function update($id)
    {
        return $this->form()->update($id);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加分类');

            $content->body($this->form());
        });
    }

    public function store()
    {
        return $this->form()->store();
    }

    public function destroy($id)
    {
        //$this->form()->destroy()
        Category::where('id',$id)->update(['deleted'=>1]);
        return ['status'=>true,'message'=>'删除成功'];
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Category::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('title', '分类名称')->rules('required');
            //$form->select('parent_id','上级分类')->options('/api/admin/categories');

            $form->image('logo','分类图片')->name(function ($file) {
                return getPicUniqueName().'.'.$file->guessExtension();
            })->removable();

            //$form->display('created_at', 'Created At');
            //$form->display('updated_at', 'Updated At');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
        });
    }
}
